<?php
/**
 * New Free Message Interface
 * 
 */
use Orx\Mailer\MailBox\Message\Receiver;
use Hive\Ressource;
use Fox\Html\Screen;
use Fox\Generator;
use Orx\Mailer\MailBox\Message\Editor\Classic;
$xview = Xview::instance();

$rid = $xview->val('rid');
// Check if the user has at least one mailer
$mailers = User::current()->personalMailers();
if(count($mailers) == 0) {
	$xview->redirect(Generator::url('messages'));
	exit;
}
$deletion = $xview->val('deletion',null,true);
$sending = $xview->val('sending',null,true);
$answerid = $xview->val('answerid');
$summary = $xview->val('summary');
if(empty($answerid)) {
	$answerid = $xview->val('newmessageid',null,true);
}
$_SESSION['newmessageid'] = null;
$_SESSION['sending'] = null;
$_SESSION['deletion'] = null;
// Check if the message has been sent or deleted
if($deletion) {
	
	if(!empty($rid)) {
		echo $xview->module(Generator::url('messages','list',null,
								array('area' => 'contact',
								'reference' => $rid,
								'summary' => $summary,
								'container' => 'messages-container')));
	} else {
		echo $xview->module(Generator::url('messages','list',null,array('status'=> 'inbox')));	
	}
	exit;
} elseif($sending) {
	
	if(!empty($rid)) {
		echo $xview->module(Generator::url('messages','list',null,
								array('area' => 'contact',
								'reference' => $rid,
								'summary' => $summary,
								'container' => 'messages-container')));
	} else {
		echo $xview->module(Generator::url('messages','viewer',null,array('messageid'=> $answerid)));
	}
	exit;
} else {
	
}

if(!empty($answerid)) {
	try {
		$answer = new Orx\Mailer\MailBox\Message($answerid);
	} catch(Fox\Object\Exception\NotExist $e) {
		$answer = new Orx\Mailer\MailBox\Message();
	}
} else {
	// Message From Contact Screen
	$answer = new Orx\Mailer\MailBox\Message();
	if(!empty($rid)) {
		try {
			$contact = Fox\Dao::load(Ressource::className(), $rid);
			$receiver = new Receiver();
			$receiver->setFields(array(
				'email' => $contact->primaryEmail(),
				'name' => $contact->name(),
				'reference' => $contact->getId(),
				'reference_class_id' => Ressource::MESSAGE_REFERENCE,
			));
			$answer->addToReceivers($receiver);
		
		} catch(Fox\Object\Exception\NotExist $e) {
			
		}
	}
	
}

$editor = new Orx\Mailer\MailBox\Message\Editor\Classic($answer);
$editor->hidden(false)
		->renderingMode(Classic::RENDERING_MODE_NEW_MESSAGE);

$screen = new \Fox\Html\Screen('answer-screen','send',null,true,\Fox\Generator::ajaxReload('viewer-container'));
if($screen->submitted()) {
	if(!$screen->errors()) {
		$editor->parsePostResult();
		exit;
	}
}

$html = $editor->render();
// 
$sourceBehavior = new Fox\Behaviors(Fox\Behaviors::source(Fox\Generator::url('messages','new',null,$_GET)));
$html = '<div id="viewer-container" class="'.$sourceBehavior->serialize($answer,'reload').'">'.$html.'</div>';
\Fox\Ressources::instance()->register(new \Fox\Ressources\Script(_ASSETS_.'js/mailbox.js'));
$html .= \Fox\Ressources::instance()->dump();

if($xview->ajax()) {
	echo $html;
} else {
	return $html;
}